﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Golos.Helper;
using Golos.Polls.PollCache;

namespace Golos.Polls.Http
{
    public partial class PollCacheServer
    {
        private void CreateNewPoll(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if(context.Request.InputStream==null)
                {
                    statusCode = (int)HttpStatusCode.BadRequest;
                    return;
                }
                PollInfo pollInfo = PollInfo.Deserialize(context.Request.InputStream);
                pollCache.CreatePoll(pollInfo);
                statusCode = (int)HttpStatusCode.OK;
                return;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }

        private void Vote(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if (context.Request.QueryString.AllKeys.Contains("questionid") 
                    && context.Request.QueryString.AllKeys.Contains("userid") 
                    && context.Request.QueryString.AllKeys.Contains("answerid")
                    && context.Request.InputStream != null)
                {
                    Guid questionId = new Guid(context.Request.QueryString["questionid"]);
                    Guid userId = new Guid(context.Request.QueryString["userid"]);
                    Guid answerId = new Guid(context.Request.QueryString["answerid"]);
                    AnswerProperties answerProperties = AnswerProperties.Deserialize(context.Request.InputStream);
                    pollCache.Vote(questionId, answerId, userId, answerProperties);
                    statusCode = (int)HttpStatusCode.OK;
                    return;
                }
                statusCode = (int)HttpStatusCode.BadRequest;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }

        private void MergeAnswers(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if (context.Request.QueryString.AllKeys.Contains("userid")
                    && context.Request.InputStream != null)
                {
                    Guid userId = new Guid(context.Request.QueryString["userid"]);
                    List<Guid> mergedUserIds = new List<Guid>();
                    using(Stream stream = context.Request.InputStream)
                    {
                        using(StreamReader reader = new StreamReader(stream))
                        {
                            while (!reader.EndOfStream)
                            {
                                string id = reader.ReadLine();
                                if(string.IsNullOrEmpty(id))
                                    continue;
                                mergedUserIds.Add(new Guid(id));
                            }
                        }
                    }
                    pollCache.MergeUserPoll(userId, mergedUserIds);
                    statusCode = (int)HttpStatusCode.OK;
                    return;
                }
                statusCode = (int)HttpStatusCode.BadRequest;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }
    }
}
